<template>
  <el-form ref="deptForm" :model="form" :rules="formRules" label-width="120px">
    <el-form-item label="部门名称" prop="name">
      <el-input
        v-model="form.name"
        style="width: 80%"
        placeholder="1-50个字符"
      />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input
        v-model="form.code"
        style="width: 80%"
        placeholder="1-50个字符"
      />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width: 80%" placeholder="请选择">
        <el-option
          v-for="item in list"
          :key="item.id"
          :value="item.username"
          :label="item.username"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input
        v-model="form.introduce"
        style="width: 80%"
        placeholder="1-300个字符"
        type="textarea"
        :rows="3"
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { addDepartments, editDepartmentsById, getDepartmentsById, getDialogOptionList } from '@/api/departments'
export default {
  props: {
    isEdit: {
      type: Boolean
    },
    id: {
      type: String,
      require: true
    },
    originList: {
      type: Array,
      require: true
    }
  },
  data() {
    const existCodeValid = (rule, value, callback) => {
      // 获取所有部门的code编码
      let existCodeList = this.originList.map(item => item.code)
      // 确认是否为编辑状态
      if (this.isEdit) {
        // 通过id将自己清除出去
        existCodeList = this.originList.filter(item => item.id !== this.id).map(item => item.code)
      }
      if (existCodeList.includes(value)) {
        callback(new Error(`部门编码：${value}已存在`))
      } else {
        callback()
      }
    }
    const existNameValid = (rule, value, callback) => {
      // 通过id将自己的子部门找出来
      let existNameList = this.originList.filter(item => item.pid === this.id).map(item => item.name)
      // 确认是否为编辑状态
      if (this.isEdit) {
        // 添加或者修改状态 同级不能有相同名字的部门
        // 找到修改状态的部门 的 pid
        const pid = this.originList.find(item => item.id === this.id).pid
        // 将同一级的部门名称添加到统一数组
        const brothers = this.originList.filter(item => item.pid === pid)
        existNameList = brothers.filter(item => item.id !== this.id).map(item => item.name)
      }
      if (existNameList.includes(value)) {
        callback(new Error(`部门名字：${value}已存在`))
      } else {
        callback()
      }
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      formRules: {
        name: [
          { required: true, message: '请输入部门的名称', triggle: 'blur' },
          { min: 1, max: 50, message: '请输入1~50个字符', triggle: 'blur' },
          { validator: existNameValid, triggle: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门的编码', triggle: 'blur' },
          { min: 1, max: 50, message: '请输入1~50个字符', triggle: 'blur' },
          { validator: existCodeValid, triggle: 'blur' }
        ],
        manager: [
          { required: true, message: '请选择部门的管理者', triggle: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '请输入部门的介绍', triggle: 'blur' },
          { min: 1, max: 50, message: '请输入1~50个字符', triggle: 'blur' }
        ]
      },
      list: []
    }
  },
  async created() {
    const res = await getDialogOptionList()
    console.log('获取员工简单列表', res.data.data)
    this.list = res.data.data
    if (this.isEdit) { // 若加载的为修改对话框
      // 根据ID查询部门详情
      const { data } = await getDepartmentsById(this.id)
      console.log('查询部门详情', data.data)
      // 回显
      this.form = data.data
    }
  },
  methods: {
    // 新增部门
    async hAdd() {
      // 发送请求
      const res = await addDepartments({ ...this.form, pid: this.id })
      console.log('部门新增成功', res.data.data)
      // 关闭对话框加重新加载渲染
      this.$emit('success')
    },
    // 根据ID修改部门详情
    async hEdit() {
      // 根据ID修改部门详情
      const res = await editDepartmentsById(this.form)
      console.log('更新部门详情成功', res.data.data)
      // 关闭对话框加重新加载渲染
      this.$emit('success')
    },
    // 提交请求
    hSubmit() {
      this.isEdit ? this.hEdit() : this.hAdd()
    },
    // 取消对话框
    hCancel() {
      this.$emit('close')
    }
  }
}
</script>

<style>
</style>
